package libra.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import libra.model.Provider_Pay;

@RestController
@RequestMapping("consumer")
public class ConsumerReqController {
//	@Autowired
//	RestTemplate restTemplate;
	@Autowired
	OpenFeignService openFeign;
	
	@Value("${server.servlet.context-path}")
	String from;
	
//	String urlPrefix = "http://127.0.0.1:20310/signle_provider_payment_20310";
	String urlPrefix = "http://CLUSTER-PROVIDER-PAYMENT/cluster_provider_payment";
	
	@RequestMapping("create")
	public Object create( HttpServletRequest req ){
		return openFeign.create(from + "_@_" + Provider_Pay.genUuid());
	}
	
	@RequestMapping("find")
	public Object find( Integer id, HttpServletRequest req ){
		return openFeign.find(id);
	}
	@RequestMapping("list")
	public Object list( HttpServletRequest req ){
		return openFeign.list();
	}
	@RequestMapping("feignTimeOut")
	public Object feignTimeOut( HttpServletRequest req ){
		return openFeign.feignTimeOut();
	}
	
	@Autowired
	DiscoveryClient discoveryClient;
	@RequestMapping("DiscoveryClient")
	public Object discoveryClient( HttpServletRequest req ){
		Map<String, Object> result = new HashMap<String, Object>();
		
		// 获取EurekaServer的注册服务列表
		List<String> services = discoveryClient.getServices();
		System.out.println( "discoveryClient.getServices() -> "+services );
		for (String serviceName : services) {
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			result.put(serviceName, list);
			// 通过注册为服务名称，spring.application.name来进行服务的检索发现
			List<ServiceInstance> srvList = discoveryClient.getInstances(serviceName);
			for( ServiceInstance si : srvList ) {
				Map<String, Object> instance = new HashMap<String, Object>();
				list.add(instance);
				
				instance.put("getServiceId", si.getServiceId());
				instance.put("getHost", si.getHost());
				instance.put("getPort", si.getPort());
				instance.put("getUri", si.getUri());
				instance.put("getInstanceId", si.getInstanceId());
				instance.put("getScheme", si.getScheme());
				instance.put("getMetadata", si.getMetadata());
				System.out.println(instance);
			}
		}
		return result;
	}
	
	// 测试OpenFeign的传参
	@RequestMapping("getParam")
	public Object getParam( Integer getParam ){
		return openFeign.getParam(getParam);
	}
	// 测试OpenFeign的传参
	@RequestMapping("postParam")
	public Object postParam( Double postParam ){
		return openFeign.postParam(postParam);
	}
	
	// 测试OpenFeign的restful传参
	@RequestMapping("getPath/{getPathParam}")
	public Object getPath( @PathVariable("getPathParam")Map<String, Object> para ) {
		return openFeign.getPath(para);
	}
	// 测试OpenFeign的restful传参
	@RequestMapping("postPath/{postPathParam}")
	public Object postPath( @PathVariable("postPathParam")Boolean para ) {
		return openFeign.postPath(para);
	}
	
	// 测试OpenFeign的发送json
	@PostMapping("postJson")
	public Object postJson( @RequestBody Map<String, Object> json ) {
		return openFeign.postJson(json);
	}
	
	// 测试OpenFeign的发送json
	@PostMapping("postObj")
	public Object postObj(  ) {
		return openFeign.postObj(new Provider_Pay(from + "【测试Obj模拟Form表单传参】"));
	}
}
